package com.ly.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ass.llm.chat.LLMChatAbstract;
import com.ass.llm.chat.factory.LLMFactory;
import com.ass.llm.dto.ChatInput;
import com.ass.llm.entity.LlmMessage;
import com.common.core.utils.IdUtils;
import com.common.security.utils.SecurityUtils;
import com.ly.system.mapper.LlmMessageMapper;
import com.ly.system.service.LlmMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * 生成管理/代码生成字段 服务类
 * @author YuSheng
 * @time 2022-08-02 14:14
 */
@Service
@Validated
public class LlmMessageServiceImpl implements LlmMessageService {


    @Autowired
    private LlmMessageMapper llmMessageMapper;

    @Autowired
    private LLMFactory llmFactory;



    /**
     * 修改
     * @author
     * @time 2022-08-02 14:14
     * @param entity
     * @return com.sx.common.ResponseResult
     */
    public void updateByConversationId(LlmMessage entity) {
        llmMessageMapper.updateByConversationId(entity);
    }


    public LlmMessage findMessageByConversationId(String conversationId) {
        return llmMessageMapper.findMessageByConversationId(conversationId);
    }

    @Override
    public void llmMessage(Boolean isInsert, LlmMessage llmMessage) {
        if (isInsert) {
            llmMessage.setId(IdUtil.getSnowflakeNextId());
            llmMessageMapper.saveMessage(llmMessage);
        } else {
            //编辑
            llmMessageMapper.updateByConversationId(llmMessage);
        }
    }

    @Override
    public String mtdChat(ChatInput chatInput) {
        LLMChatAbstract llmChat = llmFactory.getLLMByLower(chatInput.getPlatform());
        return llmChat.mtdChat(chatInput,(entity,isInsert)->
                        this.llmMessage(isInsert,entity)
                , this::findMessageByConversationId
        );
    }



}
